package com.smartwasser.yunhtgl.apphtgl.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.smartwasser.yunhtgl.apphtgl.R;
import com.smartwasser.yunhtgl.apphtgl.bean.SP5Bean.PageBean.ListBean;
import com.smartwasser.yunhtgl.apphtgl.utils.Logger;

import java.util.List;

import static com.smartwasser.yunhtgl.apphtgl.R.id.sp_item_category;
import static com.smartwasser.yunhtgl.apphtgl.utils.TypeUtils.getCategory;
import static com.smartwasser.yunhtgl.apphtgl.utils.TypeUtils.getContractStatus;

public class SPAdapter05 extends BaseAdapter {
    private Context context;
    private List<ListBean> data;

    public SPAdapter05(Context context, List<ListBean> list) {
        this.context = context;
        this.data = list;
    }

    public void refresh(List<ListBean> list) {
        this.data = list;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        ViewHolders holders;
        if (convertView == null) {
            holders = new ViewHolders();
            convertView = View.inflate(context, R.layout.sp_item, null);
            holders.sp_item_category = convertView.findViewById(sp_item_category);
            holders.sp_item_name = convertView.findViewById(R.id.sp_item_name);
            holders.sp_item_state = convertView.findViewById(R.id.sp_item_state);
            holders.sp_item_time = convertView.findViewById(R.id.sp_item_time);
            holders.sp_item_bg = convertView.findViewById(R.id.sp_item_bg);
            convertView.setTag(holders);
        } else {
            holders = (ViewHolders) convertView.getTag();
        }
        if (null != data && data.size() > 0) {
            try {
                holders.sp_item_name.setText(data.get(position).getName());
                holders.sp_item_time.setText(data.get(position).getCreateDate());
                if (data.get(position).getCategory().equals("0")) {
                    holders.sp_item_bg.setBackgroundResource(R.drawable.shape_great);
                } else if (data.get(position).getCategory().equals("1")) {
                    holders.sp_item_bg.setBackgroundResource(R.drawable.shape_normal);
                } else if (data.get(position).getCategory().equals("2")) {
                    holders.sp_item_bg.setBackgroundResource(R.drawable.shape_chapter);
                }
                holders.sp_item_state.setText(getContractStatus(data.get(position).getContractStatus()));
                holders.sp_item_category.setText(getCategory(data.get(position).getCategory()));
            } catch (Exception e) {
                Logger.e("我挂了");
            }
        }
        return convertView;
    }

    class ViewHolders {
        TextView sp_item_category, sp_item_name, sp_item_state, sp_item_time;
        RelativeLayout sp_item_bg;
    }
}